package com.lib.utils;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.text.style.SubscriptSpan;
import android.text.style.SuperscriptSpan;
import android.text.style.UnderlineSpan;

public class RichStringUtils {
	
	private SpannableStringBuilder mString;
	
	public RichStringUtils() {
		
		this("");
	}
	
	public RichStringUtils(String string) {
		
		mString = SpannableStringBuilder.valueOf(string == null ? "" : string);
	}
	
	public RichStringUtils append(String s) {
		
		mString.append(s);
		return this;
	}
	
	public SpannableStringBuilder getRichString() {
		
		return mString;
	}
	
	public RichStringUtils setForegroundColor(int Color, int start, int end) {
		// 设置前景色
		int length = mString.length();
		if (end > length) {
			end = length;
		}
		mString.setSpan(new ForegroundColorSpan(Color), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
		return this;
	}
	
	public RichStringUtils setBackgroundColor(int Color, int start, int end) {
		// 设置背景色
		int length = mString.length();
		if (end > length) {
			end = length;
		}
		mString.setSpan(new BackgroundColorSpan(Color), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
		return this;
	}
	
	public RichStringUtils setAbsoluteSize(int size, boolean dp, int start, int end) {
		// 第二个参数boolean dip，如果为true，表示前面的字体大小单位为dip，否则为像素，同上。
		int length = mString.length();
		if (end > length) {
			end = length;
		}
		mString.setSpan(new AbsoluteSizeSpan(size, dp), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
		return this;
	}
	
	public RichStringUtils setRelativeSize(float size, int start, int end) {
		// 0.5f表示默认字体大小的一半
		int length = mString.length();
		if (end > length) {
			end = length;
		}
		mString.setSpan(new RelativeSizeSpan(size), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
		return this;
	}
	
	public RichStringUtils setStyle(int style, int start, int end) {
		// 设置样式
		int length = mString.length();
		if (end > length) {
			end = length;
		}
		mString.setSpan(new StyleSpan(style), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
		return this;
	}
	
	public RichStringUtils setUnderlineSpan(int start, int end) {
		// 设置下划线
		int length = mString.length();
		if (end > length) {
			end = length;
		}
		mString.setSpan(new UnderlineSpan(), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
		return this;
	}
	
	public RichStringUtils setStrikeThroughSpan(int start, int end) {
		// 设置删除线
		int length = mString.length();
		if (end > length) {
			end = length;
		}
		mString.setSpan(new StrikethroughSpan(), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
		return this;
	}
	
	public RichStringUtils setSuperScriptSpan(int start, int end) {
		// 设置上标
		int length = mString.length();
		if (end > length) {
			end = length;
		}
		mString.setSpan(new SuperscriptSpan(), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
		return this;
	}
	
	public RichStringUtils setSubScriptSpan(int start, int end) {
		// 设置下标
		int length = mString.length();
		if (end > length) {
			end = length;
		}
		mString.setSpan(new SubscriptSpan(), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
		return this;
	}
	
	public RichStringUtils setImageSpan(Context context, int resId, int start, int end) {
		// 设置图片
		int length = mString.length();
		if (end > length) {
			end = length;
		}
		mString.setSpan(new ImageSpan(context, resId), start, end, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
		return this;
	}
}